package com.example.pattern.home

import android.annotation.SuppressLint
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

class ScaleLayoutManager(context: Context,orientation:Int = RecyclerView.HORIZONTAL,reverseLayout:Boolean=false):LinearLayoutManager(context, orientation, reverseLayout) {
    //子控件布局时
    override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
        super.onLayoutChildren(recycler, state)
        //完成缩放功能
        scaleChild()
    }
//横向滚动
    @SuppressLint("SuspiciousIndentation")
    override fun scrollHorizontallyBy(
        dx: Int,
        recycler: RecyclerView.Recycler?,
        state: RecyclerView.State?
    ): Int {
    val distance = super.scrollHorizontallyBy(dx, recycler, state)
    scaleChild()
        return distance
    }
    fun scaleChild(){
        val center = width/2f
        for(i in 0 until childCount){
            val child = getChildAt(i)!!
            val childCenter = (child.left+child.right)/2f
            val distance = abs(center-childCenter)
            val scale =1- distance/center*0.1f
            child.scaleX=scale
            child.scaleY=scale
        }
    }
}